
package numarproj;

import java.awt.CardLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Seventh
 */
public class Main extends javax.swing.JFrame {

    Connection conn = null;

    CardLayout cl;
    javax.swing.JPanel mainPanel;
    CadastrarView cadView;

    final static String MAINPANEL = "Main Panel";
    final static String CADPANEL = "Cadastrar Panel";
    //CriarView novoPesq;
    //AtualizarView editPesq;
    //PesquisarView verPesq;
    //RemoverView removePesq;
    /** Creates new form MainView */
    public Main() {
        initComponents();
        
        cl = new CardLayout();
        mainPanel = new javax.swing.JPanel();
        cadView = new CadastrarView();

        PPrincipal.setLayout(cl);
        PPrincipal.add(mainPanel, MAINPANEL);
        PPrincipal.add(cadView, CADPANEL);

        cl.show(PPrincipal, MAINPANEL);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PPrincipal = new javax.swing.JPanel();
        BarraM = new javax.swing.JMenuBar();
        MServidor = new javax.swing.JMenu();
        MSConectar = new javax.swing.JMenuItem();
        MCadastrar = new javax.swing.JMenu();
        MCadAssistido = new javax.swing.JMenuItem();
        MCadFilho = new javax.swing.JMenuItem();
        MCadNucleoF = new javax.swing.JMenuItem();
        MConsultar = new javax.swing.JMenu();
        MFerramentas = new javax.swing.JMenu();
        MFRelatorios = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        MAjuda = new javax.swing.JMenu();
        MAManual = new javax.swing.JMenuItem();
        MASobre = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout PPrincipalLayout = new javax.swing.GroupLayout(PPrincipal);
        PPrincipal.setLayout(PPrincipalLayout);
        PPrincipalLayout.setHorizontalGroup(
            PPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
        );
        PPrincipalLayout.setVerticalGroup(
            PPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 579, Short.MAX_VALUE)
        );

        MServidor.setText("Servidor");

        MSConectar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        MSConectar.setText("Conectar");
        MSConectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MSConectarActionPerformed(evt);
            }
        });
        MServidor.add(MSConectar);

        BarraM.add(MServidor);

        MCadastrar.setText("Cadastrar");

        MCadAssistido.setText("Assistido(s)");
        MCadAssistido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MCadAssistidoActionPerformed(evt);
            }
        });
        MCadastrar.add(MCadAssistido);

        MCadFilho.setText("Filho(s)");
        MCadastrar.add(MCadFilho);

        MCadNucleoF.setText("Núcleo(s) Familiar(es)");
        MCadastrar.add(MCadNucleoF);

        BarraM.add(MCadastrar);

        MConsultar.setText("Consultar");
        BarraM.add(MConsultar);

        MFerramentas.setText("Ferramentas");

        MFRelatorios.setText("Relatórios");

        jMenuItem1.setText("por atividade");
        MFRelatorios.add(jMenuItem1);

        jMenuItem2.setText("por idade");
        MFRelatorios.add(jMenuItem2);

        jMenuItem3.setText("por sexo");
        MFRelatorios.add(jMenuItem3);

        MFerramentas.add(MFRelatorios);

        BarraM.add(MFerramentas);

        MAjuda.setText("Ajuda");

        MAManual.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        MAManual.setText("Manual");
        MAManual.setEnabled(false);
        MAjuda.add(MAManual);

        MASobre.setText("Sobre");
        MAjuda.add(MASobre);

        BarraM.add(MAjuda);

        setJMenuBar(BarraM);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(PPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(PPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void MSConectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MSConectarActionPerformed
        ConectarView login = new ConectarView(this);
        login.setVisible(true);
        String id = login._uid.getText();
        char pwArray[] = new char[20]; //gfanumar
        pwArray = login._pwd.getPassword();
        String pw = TratamentoDados.parseCharVec(pwArray);

        if (conn == null && !id.isEmpty()) {

            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                System.out.println("System: Driver JDBC registrado com SUCESSO");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Não foi possível registrar o driver JDBC!", "Erro", JOptionPane.ERROR_MESSAGE);
            }

            try {
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/numar",
                id, pw);
                System.out.println("System: Conexão ao banco feita com SUCESSO");
            } catch (SQLException ex) {
                System.out.println("SQLException: " + ex.getMessage());
                System.out.println("SQLState: " + ex.getSQLState());
                System.out.println("VendorError: " + ex.getErrorCode());
            }
        }
        else if (!id.isEmpty() && login.isVisible()==true) {
            JOptionPane.showMessageDialog(null, "Ja conectado!", "Erro", JOptionPane.ERROR_MESSAGE);
        }
        try {
            //editPesq.conn = conn;
            //novoPesq.conn = conn;
            //verPesq.conn = conn;
            //removePesq.conn = conn;
        } catch (Exception e) {
        }
    }//GEN-LAST:event_MSConectarActionPerformed

    private void MCadAssistidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MCadAssistidoActionPerformed
        cl.show(PPrincipal, CADPANEL);
        
    }//GEN-LAST:event_MCadAssistidoActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                        UIManager.getSystemLookAndFeelClassName());
                }
                catch (UnsupportedLookAndFeelException e) {
                }
                catch (ClassNotFoundException e) {
                }
                catch (InstantiationException e) {
                }
                catch (IllegalAccessException e) {
                }
                Main exec = new Main();
                exec.setVisible(true);
                exec.setResizable(false);
                exec.setTitle("Grupo Francisco de Assis (GFA) - Banco de Dados "
                        + "do Núcleo Marajoara (NUMAR)");
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar BarraM;
    private javax.swing.JMenuItem MAManual;
    private javax.swing.JMenuItem MASobre;
    private javax.swing.JMenu MAjuda;
    private javax.swing.JMenuItem MCadAssistido;
    private javax.swing.JMenuItem MCadFilho;
    private javax.swing.JMenuItem MCadNucleoF;
    private javax.swing.JMenu MCadastrar;
    private javax.swing.JMenu MConsultar;
    private javax.swing.JMenu MFRelatorios;
    private javax.swing.JMenu MFerramentas;
    private javax.swing.JMenuItem MSConectar;
    private javax.swing.JMenu MServidor;
    private javax.swing.JPanel PPrincipal;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    // End of variables declaration//GEN-END:variables

}
